package Register

import (
	"MedicalTreatment/global"
	model "MedicalTreatment/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
	"time"
)

func RegiserMan(r *gin.Engine) {
	r.POST("/regiser", func(context *gin.Context) {
		var doc_pat model.Register
		if err := context.ShouldBindJSON(&doc_pat); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		var doctorPrice float64
		if err := global.DB.Debug().Model(model.Docket{}).Select("price").Where("id = ?", doc_pat.Doc_id).First(&doctorPrice).Error; err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		fmt.Println(doctorPrice)
		var patBlance float64
		if err := global.DB.Model(&model.Patient{}).Select("patient_balance").Where("id = ?", doc_pat.Pat_id).First(&patBlance).Error; err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		fmt.Println(patBlance)
		if doctorPrice > patBlance {
			context.JSON(200, gin.H{
				"Msg": "您的余额不足",
			})
		} else {
			var vip int
			if err := global.DB.Model(&model.Patient{}).Select("patient_vip").Where("id = ?", doc_pat.Pat_id).First(&vip).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
				return
			}
			fmt.Println(vip)
			var insurance int
			if err := global.DB.Model(&model.Patient{}).Select("patient_insurance").Where("id = ?", doc_pat.Pat_id).First(&insurance).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
				return
			}
			fmt.Println(insurance)
			var blance float64
			if vip == 0 && insurance == 0 {
				retain, err := strconv.ParseFloat(fmt.Sprintf("%.2f", doctorPrice*0.7), 64)
				if err != nil {
					context.JSON(200, gin.H{
						"Msg": err,
					})
				} else {
					blance = patBlance - retain
				}

			} else if vip != 0 && insurance == 0 {
				retain, err := strconv.ParseFloat(fmt.Sprintf("%.2f", doctorPrice*0.9), 64)
				if err != nil {
					context.JSON(200, gin.H{
						"Msg": err,
					})
				} else {
					blance = patBlance - retain
				}
			} else if vip == 0 && insurance != 0 {
				retain, err := strconv.ParseFloat(fmt.Sprintf("%.2f", doctorPrice*0.8), 64)
				if err != nil {
					context.JSON(200, gin.H{
						"Msg": err,
					})
				} else {
					blance = patBlance - retain
				}
			} else {

				blance = patBlance - doctorPrice
			}

			if err := global.DB.Model(model.Patient{}).Where("id = ?", doc_pat.Pat_id).Update("patient_balance", blance).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})

				return
			}

			var patient model.Patient
			var docket model.Docket
			if err := global.DB.Where("id=?", doc_pat.Pat_id).First(&patient).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
			} else {

				if err := global.DB.Where("id=?", doc_pat.Doc_id).First(&docket).Error; err != nil {
					context.JSON(200, gin.H{
						"Msg": err,
					})
				} else {
					fmt.Println("时间", doc_pat.Start_time)
					timeString := doc_pat.Start_time
					date := timeString[0:10]
					hms := timeString[11:19]
					fmt.Println(date)
					fmt.Println(hms)
					fmt.Println(timeString)
					layout1 := "15:04:05"
					result, err := time.Parse(layout1, hms)
					if err != nil {
						context.JSON(200, gin.H{
							"Msg": err,
						})
						return

					}
					endTime := result.Add(10 * time.Minute)
					endstr := endTime.Format("15:04:05")
					res := model.Register{
						Model:      gorm.Model{},
						Pat_id:     doc_pat.Pat_id,
						Doc_id:     doc_pat.Doc_id,
						Start_time: hms,
						End_time:   endstr,
						Date:       date,
					}

					if err := global.DB.Create(&res).Error; err != nil {
						context.JSON(200, gin.H{
							"Msg": err,
						})
					} else {
						context.JSON(200, gin.H{
							"Msg": "挂号成功",
						})
					}
				}
			}

		}

	})
}
